package org.jeecg.modules.wxhtml.service.impl;

import org.jeecg.modules.wxhtml.config.WxHtmlPropertiesConfig;
import org.jeecg.modules.wxhtml.enums.WxHtmlPathEnum;
import org.jeecg.modules.wxhtml.response.RefreshResponse;
import org.jeecg.modules.wxhtml.response.TokenResponse;
import org.jeecg.modules.wxhtml.response.UserInfoResponse;
import org.jeecg.modules.wxhtml.service.WxHtmlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

@Service
public class WxHtmlServiceImpl implements WxHtmlService {

    @Autowired
    private WxHtmlPropertiesConfig wxHtmlPropertiesConfig;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public TokenResponse getAccessToken(String code) {
        String url = UriComponentsBuilder
                .fromHttpUrl(wxHtmlPropertiesConfig.getUrl() + WxHtmlPathEnum.ACCESS_TOKEN.getUrl())
                .queryParam("appid", wxHtmlPropertiesConfig.getAppid())// 参数1
                .queryParam("secret", wxHtmlPropertiesConfig.getAppsecret())// 参数2
                .queryParam("code", code)// 参数3
                .queryParam("grant_type", "authorization_code")// 参数4
                .encode() // 自动编码特殊字符（如空格、中文）
                .toUriString();
        return restTemplate.getForObject(url, TokenResponse.class);
    }

    @Override
    public RefreshResponse refreshToken(String refreshToken) {
        String url = UriComponentsBuilder
                .fromHttpUrl(wxHtmlPropertiesConfig.getUrl() + WxHtmlPathEnum.REFRESH_TOKEN.getUrl())
                .queryParam("appid", wxHtmlPropertiesConfig.getAppid())// 参数1
                .queryParam("grant_type", "refresh_token")// 参数2
                .queryParam("refresh_token", refreshToken)// 参数3
                .encode() // 自动编码特殊字符（如空格、中文）
                .toUriString();
        return restTemplate.getForObject(url, RefreshResponse.class);
    }

    @Override
    public UserInfoResponse snsApiUserinfo(String accessToken, String openid) {
        String url = UriComponentsBuilder
                .fromHttpUrl(wxHtmlPropertiesConfig.getUrl() + WxHtmlPathEnum.USER_INFO.getUrl())
                .queryParam("access_token", accessToken)// 参数1
                .queryParam("openid", openid)// 参数2
                .queryParam("lang", "zh_CN")// 参数3 返回国家地区语言版本，zh_CN 简体，zh_TW 繁体，en 英语
                .encode() // 自动编码特殊字符（如空格、中文）
                .toUriString();
        return restTemplate.getForObject(url, UserInfoResponse.class);
    }

    @Override
    public boolean auth(String accessToken, String openid) {
        String url = UriComponentsBuilder
                .fromHttpUrl(wxHtmlPropertiesConfig.getUrl() + WxHtmlPathEnum.AUTH.getUrl())
                .queryParam("access_token", accessToken)// 参数1
                .queryParam("openid", openid)// 参数2
                .encode() // 自动编码特殊字符（如空格、中文）
                .toUriString();
        Object o= restTemplate.getForObject(url, Object.class);
        return true;
    }

}
